﻿using System;
using Rhizome.Contract;

namespace Rhizome.Model
{
    public abstract class StateBase<TPlant> : RhizomeElement, IEventSink,
                                              IIdentityAccessor<object>
    {
    	protected StateBase(object id) : this()
    	{
    		if (id == null) throw new ArgumentNullException("id");
    		Id = id;
    	}

    	protected StateBase()
    	{
    	}

    	protected IEventSink EventSink
        {
            get { return base.GetValue(EventSyncProperty); }
        }

        protected TPlant Plant
        {
            get { return (TPlant) GetValue(PlantProperty); }
        }

        #region IEventSink Members

        void IEventSink.CastEvent(object source, object @event)
        {
            //if (Logger != null) Logger.WriteLine("{0}.CastEvent(Source = {1}, Event = {2})", this, source, @event);

            EventSink.CastEvent(this, @event);
        }

        #endregion

        #region IIdentityAccessor<object> Members

        public object Id { get; set; }

        #endregion

        

        public override T GetValue<T>(RhizomeProperty<T> property)
        {
            if (ReferenceEquals(property, EventSyncProperty))
                return (T)((object)this);

            return base.GetValue(property);
        }

        public override string ToString()
        {
            return Id.ToString();
        }
    }
}